package com.googlecode.guicehibernate.repository;

import java.io.Serializable;
import java.util.List;

import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.orm.hibernate3.HibernateOperations;
import org.springframework.transaction.annotation.Transactional;

import com.google.inject.Inject;

public abstract class HibernateRepository<T> implements Repository<T> {

	protected Logger logger = LoggerFactory.getLogger(getClass());
	
	@Inject
    protected SessionFactory sessionFactory;
	
	@Inject
    protected HibernateOperations hibernateTemplate;

	@Override
    @Transactional
    public T save(T entity) {
    		hibernateTemplate.save(entity);
        return entity;
    }

    @Override
    @Transactional
    public T update(T entity) {
    		hibernateTemplate.update(entity);
        return entity;
    }

	@Override
	@Transactional
	public T saveOrUpdate(T entity) {
		hibernateTemplate.saveOrUpdate(entity);
		return entity;
	}
    
    @Override
    @Transactional
    public void delete(Object entity) {
    		hibernateTemplate.delete(entity);
    }
    
    @SuppressWarnings("unchecked")
	protected List<T> findByCriteria(DetachedCriteria criteria) {
    		return hibernateTemplate.findByCriteria(criteria);
    }

	@Override
	public T load(Class<T> entityClass, Serializable id) {
		return hibernateTemplate.load(entityClass, id);
	}



}
